<template>
        <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="200px" size="medium">
            <el-form-item label="选择角色" prop="name">
                <el-select v-model="ruleForm.name" placeholder="请选择角色" class="w300">
                    <el-option label="管理员" value="管理员"></el-option>
                    <el-option label="运营经理" value="运营经理"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="账号" prop="account">
                <el-input type="text" v-model="ruleForm.account" class="w300"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('ruleForm')">确 定</el-button>
                <el-button @click="onCancel()">取 消</el-button>
            </el-form-item>
        </el-form>
</template>

<script>
	export default {
        name: 'editAccount',
		data () {
			return {
                flag: false,
                ruleForm: {
                    index: 0,
                    status: 1,
                    person: '',
                    date: '',
                    name: '',
                    account: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请选择角色', trigger: 'change' }
                    ],
                    account: [
                        { required: true, message: '请输入账号', trigger: 'blur' }
                    ]
                }
            }
		},
		created () {
            if (this.$state.query && this.$state.query.roleData) {
                this.flag = true,
                this.ruleForm = {
                    ...this.$state.query.roleData
                }
            }
		},
        mounted() {
            
        },
		methods: {
            onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let lsData = JSON.parse(localStorage.getItem('accountData'));
                        if(this.flag){
                            lsData[this.ruleForm.index - 1].account = this.ruleForm.account;
                            lsData[this.ruleForm.index - 1].name = this.ruleForm.name;
                        }else{
                            this.ruleForm.index = lsData.length + 1;
                            this.ruleForm.status = 1;
                            this.ruleForm.person = 'admin';
                            this.ruleForm.date = '2020-7-21 17:12:29';
                            lsData.push(this.ruleForm);
                        }
                        localStorage.setItem('accountData', JSON.stringify(lsData));
                        this.$message.success('修改成功!')
                        this.$state.query.callback()
                        this.$modal.close()
                    } else {
                        return false;
                    }
                });
            },
            onCancel () {
                this.$modal.close();
            }
        }
	}
</script>

<style scoped>
.w300{width: 300px;}
</style>
